﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using KqAutoDesign.Base;
using KqAutoDesign.Models;

namespace KqAutoDesign
{
    public class ProjectInfo : NotifyBase, IGetGlobalParams
    {
        public ProjectInfo()
        {
            Tasks = new ObservableCollection<TaskInfo>();
            _cTskId = 0;
        }

        private string _workDir;
        public string WorkDir
        {
            get { return _workDir; }
            set
            {
                if (value != _workDir)
                {
                    _workDir = value;
                    OnPropertyChanged(nameof(WorkDir));
                }
            }
        }
        /// <summary>
        /// 动作标记，表示该项目是否已经按了停止、暂停键
        /// </summary>
        public ActionMarkEnum ActionMark { get; set; } = ActionMarkEnum.Null;


        private ProjectStateEnum _prjStateEnum;
        public ProjectStateEnum ProjectState
        {
            get 
            {
                return _prjStateEnum;
            }
            set
            {
                if (_prjStateEnum != value)
                {
                    _prjStateEnum = value;
                    OnPropertyChanged(nameof(ProjectState));
                    //CommandManager.InvalidateRequerySuggested();
                    Application.Current.Dispatcher.Invoke(() => CommandManager.InvalidateRequerySuggested());
                }
            }
        }

        private int _cTskId;
        public int CurrentTaskId
        {
            get => _cTskId;
            set
            {
                if(value != _cTskId)
                {
                    _cTskId = value;
                    OnPropertyChanged(nameof(CurrentTaskId));
                }
            }
        }

        public ObservableCollection<TaskInfo> Tasks { get; set; }

        public List<GroupScriptParam> GetGlobalParams()
        {
            List<GroupScriptParam> gsps = new List<GroupScriptParam>();
            string grpName = GlobalParamType.ProjectPath.GetDescription();
            ScriptParam sp = new ScriptParam();
            sp.Name = "WorkDir";
            sp.Description = "项目工作目录";
            sp.Value = WorkDir;
            sp.DefaultValue = WorkDir;
            sp.ParamType = ParamTypeEnum.StringType;
            GroupScriptParam gsp = new GroupScriptParam(sp, grpName);
            gsps.Add(gsp);
            if(Tasks != null)
            {
                foreach (var tsk in Tasks)
                {
                    string name = GlobalParamType.ProjectPath.GetDescription();
                    ScriptParam scp = new ScriptParam();
                    scp.Name = "TaskDir";
                    scp.Description = tsk.Name+"任务工作目录";
                    scp.Value = tsk.Path;
                    scp.DefaultValue = tsk.Path;
                    scp.ParamType = ParamTypeEnum.StringType;
                    GroupScriptParam gp = new GroupScriptParam(scp, name);
                    gsps.Add(gp);
                }
            }
            return gsps;
        }
    }
}
